<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-10 14:01
 *
 * Version: 2.0
 */

namespace App\Modules\Store\Repositories;


use App\Models\StoreRank;
use App\Modules\Store\Validators\StoreRankValidator;
use Flower\Core\Repository\Eloquent\BaseRepository;
use Illuminate\Support\Facades\DB;

class StoreRankRepository extends BaseRepository
{
	public function model()
	{
		return StoreRank::class;
	}

	public function validator()
	{
		return StoreRankValidator::class;
	}

	/**
	 * 插入数据
	 * @param $requestData
	 * @return mixed
	 */
	public function add($requestData){
		DB::beginTransaction();
		$res =  $this->model->insert($requestData);
		$res ? DB::commit() : DB::rollBack();
		return $res;
	}


	/**
	 * 查询店铺的排名
	 */
	public function getStoreRank($requestData){

		 $date = isset($requestData['date']) ? $requestData['date'] : date('Y-m-d', time());
		 return $this->model->where(['store_id'=>$requestData['store_id']])->whereDate('created_at',$date)->first();

	}

	/**
	 * 插入排名数据
	 * @param $list
	 * @return boolean
	 */
	public function addRankList($list){
		$flag = true;
		try{
			$this->model->insert($list);
			DB::commit();
		}catch (\Exception $e){
			DB::rollBack();
			$flag = false;
		}
		return  $flag;
	}

	public function findByStoreId($storeId){
		return $this->model->with('store:id,name,logo,level')->where('store_id','=',$storeId)->whereDate('created_at',date('Y-m-d',time()))->first();
	}


	public function getAmountRate($storeId,$date,$compareDate)
	{
		return $this->model
			->selectRaw('date(rank_date) as dates, value as counts')
			->whereRaw('(store_id=' . $storeId . ' and  date(rank_date)="' . $date . '")')
			->orWhereRaw('(store_id=' . $storeId . ' and date(rank_date)="' . $compareDate . '") group by date(rank_date)')
			->get()->toArray();

	}

//		$compareDate = date( 'Y-m-d', strtotime($date.' -1 week'));
//		$list = $this->model
//			->selectRaw('date(created_at) as dates, value,')
//			->where(['store_id'=>$storeId])
//			->get()->toArray();
//		$list = array_column($list,null,'dates');
//		$newList = [
//			'yesterday_count' => isset($list[$date]) ? $list[$date]['counts'] : 0,
//			'compare_count' => isset($list[$compareDate]) ? $list[$compareDate]['counts'] : 0
//		];
//		return [
//			'count' => $newList['yesterday_count'],
//			'rate' => $newList['yesterday_count'] = $newList['compare_count']  ?  0.0 : ($newList['yesterday_count'] > $newList['compare_count'] ? round($newList['yesterday_count']/$newList['compare_count'] ,1) : round($newList['compare_count']/$newList['yesterday_count'] ,1)),
//			'status' =>$newList['yesterday_count'] = $newList['compare_count'] ? 2 :  ($newList['yesterday_count'] > $newList['compare_count'] ? 1 : 0)
//		];


	/**
	 * 同级别店铺
	 */
	public function getSameLevel($storeId,$startDate,$endDate){
		$where =$this->model->selectRaw('adcode,compare_count')->whereRaw('store_id='.$storeId.' and FROM_UNIXTIME(rank_date,\'%Y-%m-%d\') ="'.$startDate.'" ')->first();
		if(!empty($where)){
            $storeIds = $this->model->where($where->toArray())
                ->whereRaw(" FROM_UNIXTIME(rank_date,'%Y-%m-%d') >= '".$endDate."' and FROM_UNIXTIME(rank_date,'%Y-%m-%d') <= '".$startDate."'")
                ->where('store_id','<>',$storeId)
                ->get(); 
        }
		$storeIds = empty($storeIds) ? [] : array_column($storeIds->toArray(),'store_id');
		return empty($storeIds) ? [] : $storeIds;
	}
}
